package com.huike.xingxing.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.huike.xingxing.R;
import com.huike.xingxing.activity.MsgChatActivity;
import com.huike.xingxing.api.Api;
import com.huike.xingxing.bean.User;
import com.huike.xingxing.ui.CircleImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MsgFragment extends BaseFragment {
    private Context context;
    private ListView lv_msg;
    private MsgAdapter adapter;
     List<User.DataBean> dataBeanList = new ArrayList<>();
    public static RequestQueue requestQueue = null;

    public  static User.DataBean currentTarget;
    static {

        currentTarget=null;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        requestQueue= Volley.newRequestQueue(context);
        try {
            getData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private  String TAG ="---";

    void getData() throws JSONException {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user_ifno",Context.MODE_PRIVATE);
        final String data = sharedPreferences.getString("data", null);
        JSONObject param = new JSONObject();

        param.put("authtype",data);

        JsonObjectRequest request = new JsonObjectRequest(1, Api.locaApi + "/getMessageHeadList",
                param,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                        StringBuffer stringBuffer = new StringBuffer(jsonObject.toString());


                        Log.e("---", "onResponse: "+ stringBuffer.toString());
                        Gson gson = new Gson();

                        User user = gson.fromJson(stringBuffer.toString(), User.class);

                        Log.e(TAG, "onResponse: "+user.getData().size() );

                        List<User.DataBean> data1 = user.getData();

                        for (User.DataBean d : data1){
                            dataBeanList.add(d);
                            Log.e(TAG, "onResponse: "+d.toString() );
                        }
                        adapter.notifyDataSetChanged();

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });

        requestQueue.add(request);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(context, R.layout.chat_list, null);
        lv_msg = view.findViewById(R.id.lv_msg);

        adapter = new MsgAdapter();
        lv_msg.setAdapter(adapter);

        //TODO 聊天列表点击
        lv_msg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(context, MsgChatActivity.class);
                Log.e(TAG, "onItemClick: "+position );
                 currentTarget = dataBeanList.get(position-1);

                startActivity(intent);
            }
        });


        return view;
    }

    //TODO 适配器
    class MsgAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            User.DataBean dataBean = dataBeanList.get(position);

            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.lv_msglist, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_msg_name.setText(dataBean.getName());
            Glide.with(context).load(Api.url+dataBean.getHead_image()).into(holder.cv_msg_image);


            return convertView;
        }

        class ViewHolder {
            public View rootView;
            public CircleImageView cv_msg_image;
            public TextView tv_msg_name;
            public TextView tv_msg_desc;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.cv_msg_image = (CircleImageView) rootView.findViewById(R.id.cv_msg_image);
                this.tv_msg_name = (TextView) rootView.findViewById(R.id.tv_msg_name);
                this.tv_msg_desc = (TextView) rootView.findViewById(R.id.tv_msg_desc);
            }

        }
    }

}
